package com.wsj.web.action.homeSiteNews;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.FileUplaodAndManageForStorage;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.HomeSiteNews.AutoPlayForm;
import com.wsj.web.subclass.displayBean.HomeSiteNews.AutoPlayFormExtendBean;
import com.wsj.web.subclass.function.ProductsParamentObj;
import com.wsj.web.vo.homeSite.AutoPlay;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

/*******
 * 
 * @author fengbin.tw
 *
 */
public class AutoPlayAction extends BaseAction {
	
	private IHomeSiteNewsServ homeSiteNewsServ;
	
	private FileUplaodAndManageForStorage imgUpload;
	
	
	
	public FileUplaodAndManageForStorage getImgUpload() {
		return imgUpload;
	}


	public void setImgUpload(FileUplaodAndManageForStorage imgUpload) {
		this.imgUpload = imgUpload;
	}


	

	public IHomeSiteNewsServ getHomeSiteNewsServ() {
		return homeSiteNewsServ;
	}


	public void setHomeSiteNewsServ(IHomeSiteNewsServ homeSiteNewsServ) {
		this.homeSiteNewsServ = homeSiteNewsServ;
	}


	/********
	 * 跳转到添加页面
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage="autoPlay_add";
		
		return  mapping.findForward(toPage);
		
	}
	
	
	/******
	 * 添加自动播放新闻
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage = "autoPlay_add";
        AutoPlayForm autoPlayForm = (AutoPlayForm)form;
        AutoPlay autoPlay = new AutoPlay();
        BeanUtils.copyProperties(autoPlay, autoPlayForm);
        autoPlay.setId(null);
        autoPlay.setStatus(0);
        autoPlay.setGmtCreate(new Date());
        autoPlay.setGmtModified(new Date());
        
        
      //如果存在新的图片,则上传图片
		if(autoPlayForm.getFormFile()!=null  && autoPlayForm.getFormFile().getFileName()!=null && !autoPlayForm.getFormFile().getFileName().equals("")){
			
			String fileName=this.addImg(autoPlayForm, request, null);
			autoPlay.setPic(fileName);
		}
		
        boolean isok = this.getHomeSiteNewsServ().addAutoPlay(autoPlay);
        if(isok){
        	request.setAttribute("msg", "添加自动播放新闻成功!");
        }else{
        	request.setAttribute("msg", "添加自动播放新闻失败!");
        }
        
        return mapping.findForward(toPage);
		
	}
	
	
	
	/********
	 * 跳转到编辑页面,编辑自动播放新闻
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		
		String toPage = "autoPlay_edit";
		String idStr = request.getParameter("id");
		String backUrl = request.getParameter("backUrl");
		request.setAttribute("backUrl", backUrl);
		Integer id = null;
		if(idStr!=null && !idStr.equals("")){
			id = Integer.valueOf(idStr);
		}
		/*******
		 * 获取自动播放新闻
		 */
		AutoPlay autoPlay= this.getHomeSiteNewsServ().getAutoPlayById(id);
		
		if(autoPlay!=null){
			request.setAttribute("autoPlay", autoPlay);
		}else{
			request.setAttribute("msg", "加载自动播放新闻对象失败!");
		}
		
		return  mapping.findForward(toPage);
		
	}
	
	
	/*******
	 * 删除自动播放新闻
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String toPage ="autoPlay_list";
		String backUrl = request.getParameter("backUrl");
		
		String idStr = request.getParameter("id");
		
		if(idStr!=null  && !idStr.equals("")){
			
			//通过id,删除自动播放新闻对象
			this.getHomeSiteNewsServ().delAutoPlayById(Integer.valueOf(idStr));
		}
		
		if(backUrl!=null  && !backUrl.equals("")){
			return new ActionForward(backUrl,true);
		}
		
		
		return mapping.findForward(toPage);
		
	}
	
	/*******
	 * 更新自动播放新闻
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage ="autoPlay_list";
		AutoPlayForm autoPlayForm = (AutoPlayForm)form;
		
		String backUrl = request.getParameter("backUrl");
		/*******
		 * 获取自动播放新闻
		 */
		AutoPlay autoPlay= this.getHomeSiteNewsServ().getAutoPlayById(autoPlayForm.getId());
		
		//如果存在新的图片,则上传图片
		if(autoPlayForm.getFormFile()!=null  && autoPlayForm.getFormFile().getFileName()!=null && !autoPlayForm.getFormFile().getFileName().equals("")){
			
			String fileName=this.addImg(autoPlayForm, request, null);
			autoPlay.setPic(fileName);
		}
		
		//标题
		if(autoPlayForm.getTitle()!=null  && !autoPlayForm.getTitle().trim().equals("")){
			autoPlay.setTitle(autoPlayForm.getTitle());
		}
		
		//链接
		if(autoPlayForm.getLink()!=null  && !autoPlayForm.getLink().trim().equals("")){
			autoPlay.setLink(autoPlayForm.getLink());
		}
		
		//状态
		if(autoPlayForm.getStatus()!=0){
			autoPlay.setStatus(autoPlayForm.getStatus());
		}
		
		//类型
		if(autoPlayForm.getType()!=null  && !autoPlayForm.getType().equals("")){
			autoPlay.setType(autoPlayForm.getType());
		}
		
		
		//更新时间
		autoPlay.setGmtModified(new Date());
		
		//将数据更新到数据库
		boolean isok=this.homeSiteNewsServ.updateAutoPlay(autoPlay);
		
		if(isok){
			 request.setAttribute("msg", "更新自动播放新闻成功!");
		}else{
			request.setAttribute("msg", "更新自动播放新闻失败!");
		}
		
		if(backUrl!=null  && !backUrl.equals("")){
			return new ActionForward(backUrl);
		}
		
		return mapping.findForward(toPage);
		
	}
	
	
    //上传图片
	public String addImg(AutoPlayForm autoPlayForm,HttpServletRequest request,String picstr){
		if(autoPlayForm.getFormFile()!=null&&!autoPlayForm.getFormFile().getFileName().trim().equals(""))
		{
		String uploadPath="fileUpload"+File.separator+"news"+File.separator;
		imgUpload.setMyFiles(autoPlayForm.getFormFile());
		imgUpload.setPath(uploadPath);
		imgUpload.setPassFileExtenSionName(HomeSiteNewsConstant.DEFAULT_EXT_NAME);
		String fileName =null;
		if(picstr==null)
		{
			 fileName = imgUpload.uploadFile(request, imgUpload.loadUploadFileName());
		}
		else {
			fileName=imgUpload.uploadFile(request, picstr);	
		}
		
		return fileName;
		}
		return null;
		
	}
	
	
	/*****
	 * 搜索
	 */
	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
	{
		String toPage ="autoPlay_list_page";
		
        this.loadAutoPlayList(request);
		
		return mapping.findForward(toPage);
		
	}

	
    /******
     * 获取自动播放列表
     * 关键字 和分页
     * @param request
     */
	private void loadAutoPlayList(HttpServletRequest request) {
		
          try {
			ProductsParamentObj parament=new ProductsParamentObj();
			  
			  String[] ret=parament.dealWithParaments(request,
					  new String[]{PageContent.page,PageContent.pageSize,PageContent.doit,
					  PageContent.backPage,PageContent.nextPage,"id","keyWord"});

			  
			  int page=PageContent.pageValue;
			  
			  int pageSize=PageContent.pageSizeValue;
			  
			  int id=0;
			  
			  
			  
			  String doit="";
			  
			  //页码
			  if(ret[0]!=null)
			  {
			      page=Integer.parseInt(ret[0]);
			  }
			  
			  
			  //显示数
			  if(ret[1]!=null)
			  {
			      pageSize=Integer.parseInt(ret[1]);
			  }  
			  
			  
			  //跳转
			  if(ret[2]!=null)
			  {
			      doit=ret[2];
			  }
			  
			  //上一页
			  if(doit!=null&&doit.equals(PageContent.backPage)&&ret[3]!=null&&!ret[3].equals(""))
			  {
			      page=Integer.parseInt(ret[3]);
			  }
			  
			  //下一页
			  if(doit!=null&&doit.equals(PageContent.nextPage)&&ret[4]!=null&&!ret[4].equals(""))
			  {
			      page=Integer.parseInt(ret[4]);
			  }
			  
			  /****
			   * id
			   */
			  if(ret[5]!=null && !ret[5].equals("")){
				  id = Integer.parseInt(ret[5]);
			  }
			  
			  String keyword="";
			  //页码
			  if(ret[6]!=null && !ret[6].endsWith(""))
			  {
				  try {
					keyword=new String(ret[6].trim().getBytes("ISO-8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			  }
			  
			  AutoPlayFormExtendBean autoPlayFormExtendBean = new AutoPlayFormExtendBean();
			  if(id!=0){
				  autoPlayFormExtendBean.setId(id);  
			  }
			  
			  autoPlayFormExtendBean.setKeyWord(keyword);
			  autoPlayFormExtendBean.setPage(page);
			  autoPlayFormExtendBean.setPageSize(pageSize);
			  
			  PageResultSet pageResultSet=this.getHomeSiteNewsServ().loadAutoPlayList(autoPlayFormExtendBean);
			  if(pageResultSet!=null)
			  {
				  	// 设置上一页
					request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
					//下一页
					request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
					//当前页
					request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
					
					//总共页数
					request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
					//大小
					request.setAttribute(PageContent.pageSize,pageResultSet.getPageSize());
					
					request.setAttribute(PageContent.recordNum, pageResultSet.getRecordNum());
					
				   request.setAttribute("id", id);
				   request.setAttribute("keyword", keyword);
				   request.setAttribute("page",page);
					
					//结果集
					List result=null;
					if(pageResultSet!=null)
					{
						result =pageResultSet.getResult();
					}
					request.setAttribute("autoPlayList", result);
					
					
			}
			  
		} catch (Exception e) {
			e.printStackTrace();
		} 
	      
	      
		
	}

}
